package me.lwn.auth.common;

import java.util.ArrayList;
import java.util.List;

public class PageResponse<T> {

    public static final String ASC = "asc";
    public static final String DESC = "desc";

    public static final int DEFAULT_PAGE_SIZE = 20;

    private long page = 1L;
    private long size = DEFAULT_PAGE_SIZE;
    private long total = 0;
    private List<T> list = new ArrayList<>();

    public PageResponse() {
    }

    public PageResponse(final long page) {
        setPage(page);
    }

    public PageResponse(final long page, final long size) {
        setPage(page);
        setSize(size);
    }

    public long getOffset() {
        return ((page - 1) * size);
    }

    public long getTotalPages() {
        if (total == 0) {
            return 0;
        }
        long count = total / size;
        return total % size > 0 ? ++count : count;
    }

    public boolean hasNext() {
        return page < getTotalPages();
    }

    public long getNextPage() {
        return hasNext() ? page + 1 : page;
    }

    public boolean hasPre() {
        return page > 1;
    }

    public long getPrePage() {
        return hasPre() ? page - 1 : page;
    }

    public long getPage() {
        return page;
    }

    public void setPage(long page) {
        this.page = page;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
